package com.ry.aspect;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;
import com.ry.bean.People;
import com.ry.util.MessageUtil;

/**
 * 处理菜单
 * @author Administrator
 *
 */
@Aspect
@Component
public class ModuleAspect {

	@Autowired
	private MessageUtil messageUtil;

	// 配置织入点
	@Pointcut("@annotation(com.ry.aspect.annotation.MenuResult)")
	public void doOperation() {

	}

	/*@AfterReturning(pointcut="doOperation()", returning="args")
	public void afterResult(JoinPoint point, List<Map<String,String>> args) {
		System.out.println(args);
		for (Map<String, String> m : args) {
			String menuName = m.get("menuName");
			String menuNamei18 = messageUtil.get(menuName);
			m.put("menuName", menuNamei18);
		}
		System.out.println(args);
	}*/
	
	@AfterReturning(pointcut="doOperation()", returning="args")
	public void afterResult(JoinPoint point, List<People> args) {
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = servletRequestAttributes.getRequest();
		String language = request.getHeader("Accept-Language");
		
		long start = System.currentTimeMillis();
		for (People people : args) {
			String lm = people.getLangMapping();
			JSONObject json = JSONObject.parseObject(lm);
			String menuName = (String) json.get(language);
			people.setName(menuName);
		}
		long end = System.currentTimeMillis();
		System.out.println(end-start);
		
	}
	
	public <T> T getCla(Class<T> cla) {
		
//		Class
		
		return null;
		
	}

}
